<?php
/**
 * Database abstraction class.
 * Works with SQLite only at the moment.
 */
class AtbudgetDB extends SQLite3 {
	function __construct($fname)
	{
		$this->open($fname);
		$this->busyTimeout(2000);
	}
}

	$db = new AtbudgetDB('../atbudget.sqlite');
	try {
		switch ($_SERVER['REQUEST_METHOD']) {
			case 'GET': //list categories
				header('Content-type: application/json');
				$cats = array();
				//list of subcategories for particular category
				if ((isset($_GET['node'])) &&($_GET['node'] != '')) {
					$res = $db->query('SELECT *, "true" AS leaf, "atb-no-icon" AS iconCls FROM category WHERE parentid = '.intval($_GET['node']).' ORDER BY name');
					if ($res) {
						while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
							$cats[] = $row;
						}
					}
				} else {//list of all categories
					$res = $db->query('SELECT *, "atb-no-icon" AS iconCls FROM category WHERE parentid = 0 ORDER BY name');
					if ($res) {
						while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
							$row['categories'] = array();
							$res1 = $db->query('SELECT *, "true" AS leaf, "atb-no-icon" AS iconCls FROM category WHERE parentid='.$row['id'].' ORDER BY name');
							while ($row1 = $res1->fetchArray(SQLITE3_ASSOC)) {
								$row['categories'][] = $row1;
							}
							$cats[] = $row;
						}
					}
				}
				echo json_encode(array('success' => 'true', 'categories' => $cats));
				break;
			case 'POST': //new category
				header('Content-type: application/json');
				$cat = json_decode($HTTP_RAW_POST_DATA);
				//
				// TODO: Check that parentid is an ID of topmost category (we do not allow depth more than 2)
				//
				if (@$db->exec('INSERT INTO category (name, parentid) VALUES ("'.$cat->name.'",'.$cat->parentid.')')) {
					$cats = array();
					$res = $db->query('SELECT *, "true" AS leaf, "atb-no-icon" AS iconCls FROM category WHERE parentid = '.intval($cat->parentid).' AND name ="'.$cat->name.'"');
					if ($res) {
						$row = $res->fetchArray(SQLITE3_ASSOC);
						$cats[] = $row;
					}
					echo json_encode(array('success' => 'true','categories' => $cats));
				} else {
					echo json_encode(array('success' => 'false', 'msg' => 'Failed to create record.','categories' => array()));
				}
				break;
			case 'PUT': //save account
				header('Content-type: application/json');
				echo json_encode(array('success' => 'false', 'msg' => 'Not implemented yet.', 'categories' => array()));
				break;
			case 'DELETE': //delete account
				header('Content-type: application/json');
				echo json_encode(array('success' => 'false', 'msg' => 'Not implemented yet.', 'categories' => array()));
				break;
			default:
				header('Content-type: application/json');
				echo json_encode(array('success' => 'false', 'msg' => 'Server cannot handle your request.','categories' => array()));
		}//switch
	} catch (Exception $e) {
		header('Content-type: application/json');
		echo json_encode(array('success' => 'false', 'msg' => "Exception occurred.\n"+$e->getMessage(), 'categories' => array()));
	}
	$db->close();
